﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace BUS
{
    public static class Utils
    {
        private const string key = "youngj";

        #region THE ENCODING
        ///// <summary>
        ///// Encrypt string by MD5
        ///// </summary>
        ///// <param name="value">string you want encrypt</param>
        ///// <returns>string has encrypted </returns>
        //public static string EncryptMD5(this string value)
        //{
        //    if (string.IsNullOrEmpty(value))
        //        return string.Empty;
        //    var md5 = new MD5CryptoServiceProvider();
        //    var valueArray = Encoding.ASCII.GetBytes(value);
        //    var sb = new StringBuilder();

        //    for (var i = 0; i < valueArray.Length; i++)
        //    {
        //        sb.Append(valueArray[i].ToString("x2").ToLower());
        //    }
        //    return sb.ToString();
        //}

        ///// <summary>
        ///// Encrypt string by SHA-1
        ///// </summary>
        ///// <param name="value">string you want encrypt</param>
        ///// <returns>string has encrypted </returns>
        //public static string EncryptSHA1(this string value)
        //{
        //    if (string.IsNullOrEmpty(value))
        //        return string.Empty;

        //    var encoding = new UnicodeEncoding();
        //    var hashBytes = encoding.GetBytes(value);
        //    var sha1=new SHA1CryptoServiceProvider();
        //    var cryptPass = sha1.ComputeHash(hashBytes);
        //    var sb = new StringBuilder();

        //    for (int i = 0; i < cryptPass.Length; i++)
        //    {
        //        sb.Append(cryptPass[i].ToString("").ToLower());
        //    }
        //    return sb.ToString();
        //}
        #endregion

        /// <summary>
        /// Mã hóa theo chuẩn MD5
        /// </summary>
        /// <param name="valueEncrypt">Chuổi cần mã hóa</param>
        /// <returns>Chuổi đã mã hóa</returns>
        public static string EncryptMD5(this string valueEncrypt)
        {
            if (string.IsNullOrEmpty(valueEncrypt))
                return null;
            var encryptArray = Encoding.UTF8.GetBytes(valueEncrypt);
            var md5 = new MD5CryptoServiceProvider();
            var keyArray = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
            var trip = new TripleDESCryptoServiceProvider()
                            {
                                Key = keyArray,
                                Mode = CipherMode.ECB,
                                Padding = PaddingMode.PKCS7
                            };
            var transform = trip.CreateEncryptor();
            var resultArray = transform.TransformFinalBlock(encryptArray, 0, encryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// Giải mã theo chuẩn MD5
        /// </summary>
        /// <param name="valueDecrypt">Chuổi cần giải mã</param>
        /// <returns>Chuỗi ban đầu</returns>
        public static string DecryptMD5(this string valueDecrypt)
        {
            var decryptArray = Convert.FromBase64String(valueDecrypt);
            var md5 = new MD5CryptoServiceProvider();
            var keyArray = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
            var trip = new TripleDESCryptoServiceProvider()
                           {
                               Key = keyArray,
                               Mode = CipherMode.ECB,
                               Padding = PaddingMode.PKCS7
                           };
            var transform = trip.CreateDecryptor();
            var resultArray = transform.TransformFinalBlock(decryptArray, 0, decryptArray.Length);
            return Encoding.UTF8.GetString(resultArray);
        }

    }
}
